﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;

namespace ChineseSchool
{
    public class Global : System.Web.HttpApplication
    {

        void RegisterRouter( RouteCollection rc)
        {
            rc.MapPageRoute("Home", "", "~/Default.aspx");
            rc.MapPageRoute("Home1", "Trang-chu", "~/Default.aspx");
            rc.MapPageRoute("Activities", "tieng-trung/hoat-dong-clb", "~/Web/Activities.aspx");
        }

        void Application_Start(object sender, EventArgs e)
        {
            //RegisterRouter(RouteTable.Routes);
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown
        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs       
        }

        void Session_Start(object sender, EventArgs e)
        {
            Session["Username"] = "";
            //Session["ID_Admin"] = "";
            Session["PassWord"] = "";
            Session["UserType"] = "";
            System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate
            {
                return
                    true;
            };
        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext myContext = HttpContext.Current;
            try
            {
                string MtSite = System.Configuration.ConfigurationManager.AppSettings["MtSite"];
                if (MtSite != "on")
                {
                    myContext.RewritePath("~/errorpage.aspx");
                }
                else
                {
                    string curURL = myContext.Request.Path.ToString();
                    string url = "~/" + curURL;
                    string curURL1 = curURL.Replace(".html", "");

                    String[] urlArray = curURL1.Split('/');
                    if (curURL.Contains(".html"))
                    {
                        if (curURL.Contains("/tieng-trung/"))
                        {
                            if (curURL.Contains("/minh-a/"))
                            {
                                if (curURL.Contains("/Trang-chu"))
                                {
                                    url = "~/Default.aspx";
                                }
                                else if (curURL.Contains("/Tin-tuc"))
                                {
                                    url = "~/Web/News.aspx?id=" + urlArray[3];
                                }
                                else if (curURL.Contains("/Bai-hoc"))
                                {
                                    url = "~/Web/Lessons.aspx";
                                }
                                else
                                {
                                    url = "~/Web/ViewPage.aspx?id=" + urlArray[3];
                                }
                            }
                            else if (curURL.Contains("/hoat-dong-clb/"))
                            {
                                url = "~/Web/Activities.aspx";
                            }
                            else if (curURL.Contains("/hoc-truc-tuyen"))
                            {
                                url = "~/Web/ViewPage.aspx?id=" + urlArray[3];
                            }
                            else if (curURL.Contains("/Tro-giup"))
                            {
                                url = "~/Web/ViewPage.aspx";
                            }
                            else if (curURL.Contains("/tin-tuc/"))
                            {
                                url = "~/Web/NewsDetail.aspx?id=" + urlArray[3];
                            }
                            else if (curURL.Contains("/bai-hoc/"))
                            {
                                url = "~/Web/LessonDetail.aspx?id=" + urlArray[3];
                            }
                            else if (curURL.Contains("/hoat-dong/"))
                            {
                                url = "~/Web/ActivitiesDetail.aspx?id=" + urlArray[3];
                            }
                            else if (curURL.Contains("/nhom-tin/"))
                            {
                                url = "~/Web/NewsType.aspx?id=" + urlArray[3];
                            }
                            else if (curURL.Contains("/cam-nhan-hoc-vien/"))
                            {
                                url = "~/Web/Camnhan.aspx?id=" + urlArray[3];
                            }
                            else if (curURL.Contains("/de-lai-cam-nhan"))
                            {
                                url = "~/Web/CamnhanAdd.aspx";
                            }
                        }
                        else if (curURL.Contains("/Trang-chu"))
                        {
                            url = "~/Default.aspx";
                        }
                        else if (curURL.Contains("/Register"))
                        {
                            url = "~/Login.aspx";
                        }
                        else if (curURL.Contains("/Login"))
                        {
                            url = "~/Login.aspx";
                        }
                        else if (curURL.Contains("/Logout"))
                        {
                            url = "~/Logout.aspx";
                        }
                        if (curURL.Contains("Trang-chu"))
                        {
                            url = "~/Default.aspx";
                        }
                        myContext.RewritePath(url);
                    }
                    else
                    {
                        myContext.RewritePath(url);
                    }
                }
            }
            catch
            {
                myContext.RewritePath("~/errorpage.aspx");
            }
        }
    }
}